package com.carful.xigua.modules.user.controller;

import com.carful.cache.service.SmsCacheServiceImpl;
import com.carful.v10.entity.HyInfo;
import com.carful.v10.service.V10Service;
import com.carful.xigua.modules.common.config.XiguaConfig;
import com.carful.xigua.modules.common.entity.Rest;
import com.carful.xigua.modules.common.entity.RestBody;
import com.carful.xigua.modules.common.util.JwtUtils;
import com.carful.xigua.modules.user.dto.request.ReqLoginUser;
import com.carful.xigua.modules.user.entity.User;
import com.carful.cache.service.SmsServiceImpl;
import com.carful.xigua.modules.user.service.impl.UserServiceImpl;
import io.github.linpeilie.Converter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * user/login
 *
 * @author ad
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/user/login")
public class LoginController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private Converter converter;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private SmsServiceImpl smsService;

    @Autowired
    private XiguaConfig xiguaConfig;

    @Autowired
    private SmsCacheServiceImpl smsCacheService;

    @Autowired
    private V10Service v10Service;

    /**
     * 登录 账号密码
     * @param loginUser
     * @param response
     * @return
     */
    @PostMapping("/login")
    public Rest login(@RequestBody
                                 ReqLoginUser loginUser,
                         HttpServletResponse response) {
        User user = userService.daoLogin(converter.convert(loginUser, User.class));
        // 认证通过
        String token = jwtUtils.generateToken(user.getUsername());
        response.setHeader(jwtUtils.getAccessTokenHeader(), jwtUtils.getTokenHeader());
        response.setHeader(jwtUtils.getTokenHeader(), token);
        return RestBody.okData(token);
    }

    /**
     * 登录 手机号
     * @param phone
     * @param code
     * @return
     */
    @GetMapping("/phoneLogin")
    public Rest login(@RequestParam("phone")
                         @NotBlank
                                 String phone,
                         @RequestParam("code")
                         @NotBlank
                                 String code,
                         HttpServletResponse response) {
        if (!"dev".equals(xiguaConfig.getActive()) && !code.equals(smsCacheService.get(phone))) {
            throw new RuntimeException("验证码错误或已失效");
        }
        User user = userService.phoneLogin(phone);
        // 认证通过
        String token = jwtUtils.generateToken(user.getUsername());
        response.setHeader(jwtUtils.getAccessTokenHeader(), jwtUtils.getTokenHeader());
        response.setHeader(jwtUtils.getTokenHeader(), token);

        smsCacheService.expire(phone);
        return RestBody.okData(token);
    }

    /**
     * 手机号查询会员信息
     * @param phone
     * @return
     */
    @GetMapping("/checkMember")
    public Rest checkMember(@RequestParam("phone") String phone) {
        List<HyInfo> memberInfo = v10Service.getMemberInfo(phone, null);
        if (memberInfo.size() == 0) throw new RuntimeException("没有会员信息");
        memberInfo = userService.sortHyInfo(memberInfo);
        return RestBody.okData(memberInfo.get(0));
    }

    /**
     * 手机验证码
     * @param phoneNumber
     * @return
     */
    @GetMapping("/phoneCode")
    public Rest phoneCode(@RequestParam("phoneNumber") String phoneNumber){
        String s = smsService.sentPhoneNumberCode(phoneNumber);
        return RestBody.okData(s);
    }

    /**
     * 退出接口
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/logout")
    public boolean logout(HttpServletRequest request,
                          HttpServletResponse response) {
        boolean b = userService.daoLogout(request, response);
        return b;
    }
}
